﻿using System.Diagnostics;

namespace KinonekoSoftware.Extensions.Strings.SearchModels.TrieTree
{
    [DebuggerDisplay("val:{Value}, items:{Children.Count}, depth: {Depth}, id:{GetHashCode()}")]
    public class NodeAdapter
    {
        internal NodeAdapter(Node node, NodeAdapter parent = null) 
        {
            Children = new Dictionary<char, NodeAdapter>();
            
            Node   = node;
            Parent = parent;
            Depth  = (byte)((parent?.Depth ?? 0) + 1);
        }

        internal Node        Node   { get; }
        internal byte        Depth  { get; }
        internal NodeAdapter Parent { get; }
        internal NodeAdapter Fail   { get; set; }
        
        internal Dictionary<char, NodeAdapter> Children { get; }
        
        public NodeAdapter this[char value]
        {
            get
            {
                //
                // 获取子节点。
                return Children.GetValueOrDefault(value);

            }
        }

        /// <summary>
        /// 
        /// </summary>
        internal bool IsWordEnd => Node?.IsWordEnd ?? false;
        internal bool HasChildren => Children.Count > 0;


        /// <summary>
        /// 
        /// </summary>
        internal char Value => Node?.Value ?? '$';
    }
}